import { Controller, Get, Param, Post } from '@nestjs/common';
import { StatusCode } from 'src/dto';
import { FlightService } from 'src/flight/services/flight/flight.service';
import { TotalFlightsService } from 'src/flight/services/total-flights/total-flights.service';

@Controller('flight')
export class FlightController {
  constructor(
    private readonly flightService: FlightService,
    private readonly totalFlightsService: TotalFlightsService,
  ) { }
  // 测试代码
  @Get('test')
  async getFlight() {
    return await this.totalFlightsService.getFlightsByMonth('2024', '09');
  };
  // ======================================================================
  // 获取指定月份的航班量
  @Get('total-flights/month/:year/:month')
  async getTotalFlights(@Param() param: any) {
    try {
      const res = await this.totalFlightsService.getFlightsByMonth(param.year, param.month);
      return {
        code: StatusCode.OK,
        message: '航班量获取成功！',
        data: {
          total: res._sum.total,
          pvgDep: res._sum.pvgDep,
          pvgArr: res._sum.pvgArr,
        },
      }
    } catch (error) {
      return {
        code: StatusCode.SERVICE_UNAVAILABLE,
        message: `航班量获取失败！错误信息：${error}`,
      };
    }
  };

  // 获取指定日期的航班量
  @Get('total-flights/date/:date')
  async getTotalFlightsDate(@Param() param: any) {
    try {
      const res = await this.totalFlightsService.getFlightsByDate(param.date);
      return {
        code: StatusCode.OK,
        message: '航班量获取成功！',
        data: {
          total: res.total,
          pvgDep: res.pvgDep,
          pvgArr: res.pvgArr,
        },
      }
    } catch (error) {
      return {
        code: StatusCode.SERVICE_UNAVAILABLE,
        message: `航班量获取失败！错误信息：${error}`,
      };
    }
  };

  @Get('total-flight/monthly-flights/:year/:startMonth/:endMonth')
  async getMonthlyFlights(@Param() param: any) {
    try {
      const res = await this.totalFlightsService.getMonthlyFlightStats(param.year, param.startMonth, param.endMonth);
      return {
        code: StatusCode.OK,
        message: '航班量获取成功！',
        data: res,
      }
    } catch (error) {
      return {
        code: StatusCode.SERVICE_UNAVAILABLE,
        message: `航班量获取失败！错误信息：${error}`,
      };
    }
  };

  @Get('base-flights-info/:date')
  async getBaseFlightsInfoList(@Param() param: any) {
    return await this.flightService.generateBaseFlightsInfoList(param.date);
  }


}
